package com.hdjk.view.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.hdjk.view.R;
import com.hdjk.view.fragment.main.FinancialFragment;
import com.hdjk.view.fragment.main.InvestmentFragment;
import com.hdjk.view.fragment.main.PersonalCenterFragment;
import com.hdjk.view.fragment.main.ShopCarFragment;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.component.CustomView.PagerSlidingTabStrip;
import com.lst.projectlib.component.CustomView.PagerSlidingTabStrip.IconTextTabProvider;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.PermissionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class MainTabActivity extends BaseActivity implements OnClickListener {
	private PagerSlidingTabStrip mTabs =null;
	private List<Fragment> mFramentList = null;
	private ViewPager mViewPager = null;
	private long mExitTime = 0;
	private int mCurrentPage = 0;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main_tab);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mTabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
	}

	@Override
	protected void initData() {
		mFramentList = new ArrayList<>();
		mFramentList.add(new InvestmentFragment());
		mFramentList.add(new FinancialFragment());
		mFramentList.add(new ShopCarFragment());
		mFramentList.add(new PersonalCenterFragment());
		mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		mTabs.setViewPager(mViewPager);
		setTabsValue();

		mViewPager.setOffscreenPageLimit(1);// 预加载页面的数量
		mViewPager.setCurrentItem(0);
	}

	public void setCurrentItem(int index) {
		if (mViewPager != null && mFramentList != null && index < mFramentList.size()) {
			mViewPager.setCurrentItem(index);
		}
	}

	/**
	 * 对PagerSlidingTabStrip的各项属性进行赋值。
	 */
	private void setTabsValue() {
		mTabs.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
					case 0:
						mCurrentPage = 0;
						break;
					case 1:
						mCurrentPage = 1;
						break;
					case 2:
						mCurrentPage = 2;
						break;
					case 3:
						mCurrentPage = 3;
						break;
				}

			}
		});
	}

	public class MyPagerAdapter extends FragmentPagerAdapter implements
			IconTextTabProvider {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		private final String[] titles = { "智投","理财","购物车","我的资产"};
		private final int[] iconId = {R.drawable.img_foot_0, R.drawable.img_foot_1,R.drawable.icon_gouwuche_dis, R.drawable.img_foot_2};
		private final int[] iconSelectedId = {R.drawable.img_foot_0_press, R.drawable.img_foot_1_press,R.drawable.icon_gouwuche_sel,  R.drawable.img_foot_2_press};

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			return mFramentList.get(position);
		}

		@Override
		public int getPageIconResId(int position) {
			return iconId[position];
		}

		@Override
		public int getPageSelectedIconResId(int position) {
			return iconSelectedId[position];
		}

	}

	@Override
	protected Object loadData(int messageId, Object params) {
		Msg msg = new Msg();
		switch (messageId) {
		case 1000:
			break;
		}
		return msg;
	}
	@Override
	protected void refresh(int messageId, Object params) {
		Msg msg = (Msg) params;
		switch (messageId) {
		case 1000:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// 2s内再次选择back键有效
		if (System.currentTimeMillis() - mExitTime > 2000) {
			Toast.makeText(this, "再按一次退出好得财富", Toast.LENGTH_SHORT).show();
			mExitTime = System.currentTimeMillis();
		} else {
			super.onBackPressed();
//			System.exit(0);//强制退出应用,杀死了整个进程，
		}
	}

	public void setUnReadCount(int position, int count) {
		try {
			if (count == 0) {
				mTabs.setRedDotVisible(position, false);
			} else {
				mTabs.setRedDot(position, count >= 100 ? "99+" : String.valueOf(count));
				mTabs.setRedDotVisible(position, true);
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		}
	}

	private GetPermission getPermission = new GetPermission();
    private class GetPermission implements PermissionUtils.PermissionGrant{
        private int isFrom = 0;
		private int denyLocationTimes= 0;

        @Override
        public void onPermissionGranted(int requestCode, String msg) {
            switch (requestCode) {
                case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
                    //设置授权成功后的回调，可以是一个提醒或一个操作等
                    if (isFrom == 1){        //获取到MoreFragment的定位权限
						startLocation(0);
                    }
                    break;
            }
        }

		@Override
		public void onPermissionDeny(int requestCode, String msg) {
			switch (requestCode) {
				case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
					//拒绝授权后的回调，可以是一个提醒或一个操作等
					if (isFrom == 1){        //获取到MoreFragment的定位权限
						startLocation(0);
					}

					denyLocationTimes++;
					break;
			}
		}
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, getPermission);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
			case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
				if (getPermission!=null){
					if (PermissionUtils.checkPermission(this, requestCode)){
						//设置授权成功后的回调，可以是一个提醒或一个操作等
						getPermission.onPermissionGranted(requestCode,"form app systemSetPage");
					}else{
						//拒绝授权后的回调，可以是一个提醒或一个操作等
						getPermission.onPermissionDeny(requestCode,"form app systemSetPage");
					}
				}
				break;
		}
	}

	public void getMoreFragmentLocationPermission(){
		if(getPermission.denyLocationTimes<1){
			PermissionUtils.requestPermission(MainTabActivity.this,PermissionUtils.CODE_ACCESS_FINE_LOCATION, getPermission);
		}else{
			startLocation(0);
		}
    }

	public void startLocation(int timeSpan) {
		// 开始定位
//		if (mLocationUtil != null) {
//			mLocationUtil.setTimeSpan(timeSpan);
//			mLocationUtil.startLocation();
//		}
	}
	private void stopLocation() {
		// 停止定位
//		if (mLocationUtil != null) {
//			mLocationUtil.stopLocation();
//		}
	}

	@Override
	protected void onResume() {
		super.onResume();

//		if (getPermission != null) {
//			getPermission.isFrom = 1;
//			getMoreFragmentLocationPermission();
//		}
	}
}
